package org.zky.modules.transaction.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.transaction.domain.ZkyOrderTab;
import org.zky.modules.transaction.repository.ZkyOrderTabRepository;
import org.zky.modules.transaction.service.ZkyOrderTabService;
import org.zky.modules.transaction.service.dto.ZkyOrderTabDto;
import org.zky.modules.transaction.service.dto.ZkyOrderTabQueryCriteria;
import org.zky.modules.transaction.service.mapper.ZkyOrderTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyOrderTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyOrderTabServiceImpl implements ZkyOrderTabService {

    private final ZkyOrderTabRepository zkyOrderTabRepository;

    private final ZkyOrderTabMapper zkyOrderTabMapper;

    public ZkyOrderTabServiceImpl(ZkyOrderTabRepository zkyOrderTabRepository, ZkyOrderTabMapper zkyOrderTabMapper) {
        this.zkyOrderTabRepository = zkyOrderTabRepository;
        this.zkyOrderTabMapper = zkyOrderTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyOrderTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyOrderTab> page = zkyOrderTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyOrderTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyOrderTabDto> queryAll(ZkyOrderTabQueryCriteria criteria){
        return zkyOrderTabMapper.toDto(zkyOrderTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyOrderTabDto findById(Long orderId) {
        ZkyOrderTab zkyOrderTab = zkyOrderTabRepository.findById(orderId).orElseGet(ZkyOrderTab::new);
        ValidationUtil.isNull(zkyOrderTab.getOrderId(),"ZkyOrderTab","orderId",orderId);
        return zkyOrderTabMapper.toDto(zkyOrderTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyOrderTabDto create(ZkyOrderTab resources) {
        return zkyOrderTabMapper.toDto(zkyOrderTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyOrderTab resources) {
        ZkyOrderTab zkyOrderTab = zkyOrderTabRepository.findById(resources.getOrderId()).orElseGet(ZkyOrderTab::new);
        ValidationUtil.isNull( zkyOrderTab.getOrderId(),"ZkyOrderTab","id",resources.getOrderId());
        zkyOrderTab.copy(resources);
        zkyOrderTabRepository.save(zkyOrderTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long orderId : ids) {
            zkyOrderTabRepository.deleteById(orderId);
        }
    }

    @Override
    public void download(List<ZkyOrderTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyOrderTabDto zkyOrderTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("1:PC 2:wechat", zkyOrderTab.getOrderSource());
            map.put(" userId",  zkyOrderTab.getUserId());
            map.put("user mobile", zkyOrderTab.getMobileNo());
            map.put("1:init 2:payment_success 3:payment_fail 4:cancel 5:refunding 6:refunded", zkyOrderTab.getOrderStatus());
            map.put("1:course 2:activity", zkyOrderTab.getOrderType());
            map.put("course_id or activity_id of this order", zkyOrderTab.getOrderItemId());
            map.put("now is campus_id or 0", zkyOrderTab.getOrderItemReference());
            map.put(" createTime",  zkyOrderTab.getCreateTime());
            map.put(" updateTime",  zkyOrderTab.getUpdateTime());
            map.put(" expiredTime",  zkyOrderTab.getExpiredTime());
            map.put("outer order id", zkyOrderTab.getPartnerOrderId());
            map.put("1:wechat 2:alipay", zkyOrderTab.getPaymentMethod());
            map.put(" paymentReferenceNo",  zkyOrderTab.getPaymentReferenceNo());
            map.put("amount * 100", zkyOrderTab.getAmount());
            map.put(" accountName",  zkyOrderTab.getAccountName());
            map.put(" paymentTime",  zkyOrderTab.getPaymentTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}